/**
 * 货物字典数据
 */


// 字典列表
function getList(){
    var formData = {};
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwzd/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//字符串
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].hw_code + '</td>';
        h += '<td>';
        h += '</td>';
        h += '<td>';
        h += '<a href="javascript:;" onclick="popHwEdit(' + d[i].id + ');">修改</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;">图片</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" class="del" onclick="del(' + d[i].id + ');">删除</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="code' + d[i].id + '" value="' + d[i].hw_code + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//修改货物
function popHwAdd(){
    var h = '';
    h += '<style type="text/css">.popCnt { height: 300px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">货物入仓</div>';
    h += '<div style="margin: 5px 0;">货物名称</div>';
    h += '<input type="text" id="hw_name" placeholder="输入名称" value=""/>';
    h += '<div style="margin: 5px 0;">货物号</div>';
    h += '<input type="text" id="hw_code" placeholder="输入编号" value=""/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="modify(0);">保存配置</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//修改货物
function popHwEdit(v){
    var title = $('#title' + v).val();
    var code = $('#code' + v).val();
    console.log(title, code);
    var h = '';
    h += '<style type="text/css">.popCnt { height: 300px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">货物入仓</div>';
    h += '<div style="margin: 5px 0;">货物名称</div>';
    h += '<input type="text" id="hw_name" placeholder="输入名称" value="' + title + '"/>';
    h += '<div style="margin: 5px 0;">货物号</div>';
    h += '<input type="text" id="hw_code" placeholder="输入编号" value="' + code + '"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="modify(' + v + ');">保存配置</button>';
    h += '</div>';
    $('#formPop').html(h);
}


// 保存
function modify(v){
    var hw_name = $('#hw_name').val();
    var hw_code = $('#hw_code').val();
    if (hw_name == '') {
        msgPop({msg: '名称必须的'}); return
    }
    if (hw_code == '') {
        msgPop({msg: '编号必须的'}); return
    }
    var formData = {
        id: v, title: hw_name, hw_code: hw_code,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwzd/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 删除
function del(v){
    var formData = {
        id: v, 
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwzd/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}